<?php
/**
 * Created by PhpStorm.
 * User: 秋枫雁飞
 * Date: 2019/5/10
 * Time: 11:24
 */

namespace Aiweline\Core\Helper\Random;


class Random
{
    function random_code_type($length = 8, $type = 'alpha-number')
    {
        $code_arr = array(
            'alpha' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
            'number' => '0123456789',
            'sign' => '#$%@*-_',
        );

        $type_arr = explode('-', $type);

        foreach ($type_arr as $t) {
            if (!array_key_exists($t, $code_arr)) {
                trigger_error("Can not generate type ($t) code");
            }
        }

        $chars = '';

        foreach ($type_arr as $t) {
            $chars .= $code_arr[$t];
        }
        $chars = str_shuffle($chars);
        $number = $length > strlen($chars) - 1 ? strlen($chars) - 1 : $length;
        return substr($chars, 0, $number);
    }
}